package controllers;

import entity.User;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import jdbc.Function;
import view.PersonalInformationInterface;

/**
 * @author zjw
 * @date 2020/1/8 13:54
 */
public class EditingInformationControllers {
    private User user;
    private Stage primaryStage;
    private Text idText;
    private TextArea mottoTextArea;
    private TextField nameTextField;
    private TextField ageTextField;
    private TextField addressTextField;
    private TextField telNumTextField;
    private RadioButton manRadioButton;
    private RadioButton womanRadioButton;

    private Button saveButton;
    private Button returnButton;

    public EditingInformationControllers(User user, Stage primaryStage, Text idText,
                                         TextArea mottoTextArea, TextField nameTextField,
                                         TextField ageTextField, TextField addressTextField,
                                         TextField telNumTextField, RadioButton manRadioButton,
                                         RadioButton womanRadioButton) {
        this.user = user;
        this.primaryStage = primaryStage;
        this.idText = idText;
        this.mottoTextArea = mottoTextArea;
        this.nameTextField = nameTextField;
        this.ageTextField = ageTextField;
        this.addressTextField = addressTextField;
        this.telNumTextField = telNumTextField;
        this.manRadioButton = manRadioButton;
        this.womanRadioButton = womanRadioButton;

        this.saveButton = new Button("保存");
        this.returnButton = new Button("返回");
    }

    public Button getSaveButton() {
        // 保存编辑信息，并返回
        saveButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String gender = "";
                if(manRadioButton.isSelected()) {
                    gender = manRadioButton.getText();
                } else {
                    gender = womanRadioButton.getText();
                }
                User newUser = new User(user.getId(), mottoTextArea.getText(),
                        nameTextField.getText(), ageTextField.getText(), gender,
                        addressTextField.getText(), telNumTextField.getText(), "");
                Function function = new Function();
                function.update(newUser);
                newUser = function.findUser(newUser.getId());
                primaryStage.close();
                try {
                    new PersonalInformationInterface(newUser, new Stage(), 0).start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return saveButton;
    }

    public Button getReturnButton() {
        // 退出编辑窗口
        returnButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.close();
            }
        });
        return returnButton;
    }
}
